package com.htjc.integral.account.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.htjc.integral.account.mapper.newdb.EcBpOuterSystemparamMapper;
import com.htjc.integral.common.annotation.ExecuteTime;
import com.htjc.integral.common.util.StrUtil;
import com.htjc.integral.model.account.EcBpOuterSystemparam;

import lombok.extern.slf4j.Slf4j;

/**
 * 根据key获取调用地址
 * @author zhangjiajia
 *
 */
@Slf4j(topic = "jfLog")
@Component
public class UrlConfigUtil {

	@Autowired
	private EcBpOuterSystemparamMapper ecBpOuterSystemparamMapper;
	@Autowired
	private RedisTem redisTem;
	
	/**
	 * 根据redis的key获取url地址
	 * @param prefix
	 * @param redisKey
	 * @return
	 */
	public String getUrlByKey(String prefix,String redisKey){
		String result = null;
		try {
			//先从redis获取
//			result = redisTem.getPre(prefix, redisKey);
//			if(StrUtil.isEmpty(result)){
				result = queryParamByCode(redisKey);
				if(!StrUtil.isEmpty(result)){
					//更新redis更新成最新的值。
					String flag = redisTem.setPre(prefix, redisKey, result);
					if("1".equals(flag)){
						log.debug("getUrlByKey 数据库查询之后更新redis成功！！！！");
					}
				}
//			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error("根据key获取调用地址:参数1:{},参数2:{},错误信息:{}",prefix,redisKey,e.toString());
		}
		
		return result ;
	}
	
	/**
	 * 新增请求地址配置
	 * @param prefix
	 * @param redisKey
	 * @return
	 */
	@ExecuteTime
	public String setUrlByKey(String prefix,String redisKey,String value){
		log.info("新增请求地址配置：setUrlByKey=======================》开始《===================");
		String result = null;
		int flag = 0;
		try {
			//先查询数据库数据
			String dbValue = queryParamByCode(redisKey);
			//判断是否存在此配置
			if(StrUtil.isEmpty(dbValue)){
			//插入数据库
				flag = insertParamByCode(redisKey, value);
			}else{
				//更新数据库
				flag = updateParamByCode(redisKey, value);
			}
			//数据库操作成功之后 更新redis
			if(flag==1){
				
				//更新redis更新成最新的值。
				result = redisTem.setPre(prefix, redisKey, value);
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error("根据key获取调用地址:参数1:{},参数2:{},错误信息:{}",prefix,redisKey,e.toString());
		}
		log.info("新增请求地址配置:setUrlByKey====prefix:{},key:{},value:{}=======================》结束《===================",prefix,redisKey,value);
		return result ;
	}
	
	
	
	/**
	 * 更新配置
	 * 
	 * @param code
	 * @return
	 * @throws Exception
	 */
	public int  updateParamByCode(String code,String value){
		log.info("更新配置表配置=======================》开始《===================");
		int flag = 0;
		try {
			
			EcBpOuterSystemparam record = new EcBpOuterSystemparam();
			record.setParamcode(code);
			record.setParamvalue(value);
			flag = ecBpOuterSystemparamMapper.udpate(record);
		} catch (Exception e) {
			log.error("更新配置参数异常:参数:{},错误信息:{}",code,e.toString());
		}
		
		log.info("更新配置表配置=======================》完成《===================");
		return flag;
	}
	
	/**
	 * 从参数配置表中查询配置参数
	 * 
	 * @param code
	 * @return
	 * @throws Exception
	 */
	public String queryParamByCode(String code){
//		log.info("读取资产中接口数据地址=======================》开始《===================");
		EcBpOuterSystemparam urlAddModel = null;
		try {
			 urlAddModel = ecBpOuterSystemparamMapper.selectByPrimaryParamcode(code);
		} catch (Exception e) {
			log.error("查询配置参数异常:参数:{},错误信息:{}",code,e.toString());
		}
		return urlAddModel.getParamvalue();
	}
	
	/**
	 * 插入参数表数据
	 * 
	 * @param code
	 * @return
	 * @throws Exception
	 */
	
	public int insertParamByCode(String code,String param){
		log.info("插入参数表数据=======================》开始《===================");
		int flag = 0;
		try {
			EcBpOuterSystemparam record = new EcBpOuterSystemparam();
			record.setParamcode(code);
			record.setParamvalue(param);
			
			flag = ecBpOuterSystemparamMapper.insert(record);
		
		} catch (Exception e) {
			log.error("查询配置参数异常:参数:{},错误信息:{}",code,e.toString());
		}
		
		log.info("插入参数表数据=======================》成功《===================");
		return flag;
	}
	
	
	
	//根据gameId获取配置   增加版本锁
	/*private  Map<String, GameIDConfig> gameIDMap = new HashMap<String, GameIDConfig>(0);
	private  String lockVersion = "";
	
	public GameIDConfig getConfigByGameID(String gameID){
		
		GameIDConfig result = null;
		try {
			//比较版本锁 
			String newLockVersion = redisTem.get("gameid_config_lock_version");
			if(lockVersion.equals(newLockVersion)){
				//如果版本锁相同  表示没有更新
				result = gameIDMap.get(gameID);
			}else{
				lockVersion = newLockVersion;
				Map<String, String> maplist = redisTem.hgetall("gameid_config");
				if(null!=maplist&&maplist.size()>0){
					gameIDMap.clear();
					for (String index : maplist.keySet()) {
						String value = maplist.get(index);
						String[] split = value.split(",");
						GameIDConfig config = new GameIDConfig();
						config.setGameID(split[0]);
						config.setSqlHost(split[1]);
						config.setSqlPost(split[2]);
						config.setSqlName(split[3]);
						config.setSqlPassword(split[4]);
						config.setRedisHost(split[5]);
						config.setRedisPost(split[6]);
						config.setRedisPassword(split[7]);
						config.setKey(split[8]);
//						config.setRabbitmq(split[9]);
						gameIDMap.put(index, config);
					}
					 result  = gameIDMap.get(gameID);
			}
			}
			
		} catch (Exception e) {
			System.err.println("GameIDUtil:getConfigByGameID error!!! {}"+e);
			e.printStackTrace();
		}
		
		return result ;
	}*/
	
	

	//////////////////////////////新增接口 2018.4.12/////////////////////////////////////////////////////////////////////
/*	public List<String> getChannelNameList(String prefix){
		return redisTem.lrange(prefix, RedisKey.KEY_CHANNELNAME_LIST, 0, -1);
	}
	
	public String getChannelNameByGameID(String prefix,Integer gameID){
		return redisTem.getPre(prefix, RedisKey.KEY_CHANNELNAME+gameID);
	}
	
	public String getChannelNameByGameID(String prefix,Integer gameID,Integer channelType){
		return redisTem.getPre(prefix, RedisKey.KEY_CHANNELNAME+channelType+"_"+gameID);
	}
	*/
	
/*	public boolean checkGameExist(String prefix,Integer gameID){
		boolean flag = false;
		try {
			String id = redisTem.getPre(prefix, RedisKey.KEY_GAMEID+gameID);
			if(!StrUtil.isEmpty(id)){
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return flag;
		
	}
	
	public boolean checkChannelExist(String prefix,Integer gameID,Integer channelType){
		boolean flag = false;
		try {
			String channel = redisTem.getPre(prefix, RedisKey.KEY_CHANNELNAME+channelType+"_"+gameID);
			if(!StrUtil.isEmpty(channel)){
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean checkTokenExist(String prefix,Integer gameID){
		boolean flag = false;
		try {
			String token = redisTem.getPre( prefix, RedisKey.KEY_TOKEN+gameID);
			if(!StrUtil.isEmpty(token)){
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}*/
	
	
	
	
	
	
}
